package com.xrh.xdemo.service.controllers;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.annotation.InterfacePermission;
import com.xrh.core.annotation.RepeatRequest;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.SysRoleDTO;
import com.xrh.xdemo.api.request.addRequest.SysRoleAddRequest;
import com.xrh.xdemo.api.request.queryRequest.SysRoleQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.RoleBindMenuRequest;
import com.xrh.xdemo.api.request.updateRequest.RoleBindUsersRequest;
import com.xrh.xdemo.api.request.updateRequest.SysRoleUpdateRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.service.SysRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="角色API")
@RestController
@RequestMapping("sysRole")
@Slf4j
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
   	 * @function 角色解绑用户
   	 * @param bindRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("角色解绑用户")
   	@PostMapping("/roleUnbindUsers")
 	@InterfacePermission(name="角色解绑用户" , value ="/sysRole/roleUnbindUsers")
	@OperateTrace(operateType = "解绑" , businessType = "用户角色" ,  businessNoKeyPath = "id")
   	public  CommonResult<Void> roleUnbindUsers(@Valid @RequestBody RoleBindUsersRequest bindRequest){
   		
   		try {
   		
   			sysRoleService.roleUnbindUsers(bindRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("roleUnbindUsers 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
    
    /**
   	 * @function 角色绑定用户
   	 * @param bindRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("角色绑定用户")
   	@PostMapping("/roleBindUsers")
   	@InterfacePermission(name="角色绑定用户" , value ="/sysRole/roleBindUsers")
   	@OperateTrace(operateType = "绑定" , businessType = "用户角色" ,   businessNoKeyPath = "id")
   	public  CommonResult<Void> roleBindUsers(@Valid @RequestBody RoleBindUsersRequest bindRequest){
   		
   		try {
   		
   			sysRoleService.roleBindUsers(bindRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("roleBindUsers 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 角色绑定菜单
   	 * @param bindRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("角色绑定菜单")
   	@PostMapping("/roleBindMenus")
   	@InterfacePermission(name="角色绑定菜单" , value ="/sysRole/roleBindMenus")
   	@OperateTrace(operateType = "绑定" , businessType = "角色菜单" ,   businessNoKeyPath = "id")
   	public  CommonResult<Void> roleBindMenus(@Valid @RequestBody RoleBindMenuRequest bindRequest){
   		
   		try {
   			
   			sysRoleService.roleBindMenus(bindRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("roleBindMenus 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 新增角色
   	 * @param sysRoleAddRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("新增角色")
   	@PostMapping("/addSysRole")
   	@InterfacePermission(name="新增角色" , value ="/sysRole/addSysRole")
   	@RepeatRequest(timeout = 2)
	@OperateTrace(operateType = "新增" , businessType = "角色" ,  businessNoFromReturnResult = true , businessNoKeyPath = "id" , titleKeyPath  = "roleName")
   	public  CommonResult<Long> addSysRole(@Valid @RequestBody SysRoleAddRequest sysRoleAddRequest){
   		
   		try {
   			
   			Long id = sysRoleService.addSysRole(sysRoleAddRequest);
   			
   			return CommonResult.success(id);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("addSysRole 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 删除角色
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("删除角色")
   	@ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/deleteSysRoleById")
   	@InterfacePermission(name="删除角色" , value ="/sysRole/deleteSysRoleById")
   	@OperateTrace(operateType = "删除" , businessType = "角色" , businessNoKeyPath = "id")
   	public CommonResult<Void> deleteSysRoleById(Long  id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   			
   			sysRoleService.deleteSysRoleById(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("deleteSysRole发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	
   	/**
   	 * @function 更新角色
   	 * @param sysRoleUpdateRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("更新角色")
   	@PostMapping("/updateSysRole")
   	@InterfacePermission(name="编辑角色" , value ="/sysRole/updateSysRole")
   	@OperateTrace(operateType = "修改" , businessType = "角色" , businessNoKeyPath = "id" ,  titleKeyPath = "roleName" )
   	public  CommonResult<Void> updateSysRole(@Valid @RequestBody SysRoleUpdateRequest sysRoleUpdateRequest){
   		
   		try {
   			
   			sysRoleService.updateSysRole(sysRoleUpdateRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("updateSysRole 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 根据ID获取对象
   	 * @param id
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取角色信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/findSysRoleById")
   	public CommonResult<SysRoleDTO> findSysRoleById(Long id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   				
   			SysRoleDTO dto =  sysRoleService.findSysRoleById(id);
   			
   			return CommonResult.success(dto);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysRoleById 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   		
   	}
   	
   	/**
   	 * @function 查询角色列表
   	 * @author 肖荣辉
   	 * @param sysRoleQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取角色列表")
   	@PostMapping("/findSysRoleList")
   	public CommonResult<CommonPage<SysRoleDTO>> findSysRoleList(@RequestBody  SysRoleQueryRequest sysRoleQueryRequest){
   		
   		try {
   			 
   			return sysRoleService.findSysRoleList(sysRoleQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysRoleList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}

}
